package com.example.mapping.manage.factory;

import com.example.mapping.enumeration.VisitorTypeEnum;
import com.example.mapping.manage.visit.Visitor;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * json访问者工厂
 *
 * @author baoyoujia
 * @date 2022/10/20
 */
@Component
public class JsonVisitorFactory implements Factory {

    /**
     * 策略集合
     */
    private final Map<VisitorTypeEnum, Visitor> visitorTypeEnumVisitorMap = Maps.newHashMap();

    public JsonVisitorFactory(List<Visitor> visitorList) {
        visitorList.forEach(visitor -> visitorTypeEnumVisitorMap.put(visitor.getType(), visitor));
    }

    /**
     * 工厂获取策略实现
     *
     * @param type 类型
     * @return 访问者
     */
    @Override
    public Visitor getInstance(Integer type) {
        VisitorTypeEnum visitorTypeEnum = VisitorTypeEnum.getEnum(type);
        return visitorTypeEnumVisitorMap.get(visitorTypeEnum);
    }
}
